<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;
use App\Models\Memberorders;
use App\Models\Documents;
use Auth,Response,MarkdownEditor;

class DocumentsController extends Controller
{
    public function documents(Request $request,$id=1)
    {
        $users = Auth::user();
        $documentdetial = Documents::getDocumentsDetial($id);
        if (empty($documentdetial)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","文档不存在,请检查链接是否正确!");        
        if ($documentdetial->authority != 1 && $users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权查看本文档!");
        $memberOrder = Memberorders::getLastLinksOrders($users->id);
        $product_id = isset($memberOrder->product_id) ? $memberOrder->product_id : 1;
        $documents = Documents::getDocumentsTitle();
        if (empty($documents)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","文档不存在,请检查链接是否正确!");
        $partners = $documents->where("documents_id","P")->all();
        $menus = [];
        foreach ($partners as $partner) {
            $childs = $partners = $documents->where("documents_id",$partner->id)->toArray();
            $menus[] =  ["id" => $partner->id,"title" => $partner->title,"documents_id" => $partner->documents_id,"product_id" => $partner->product_id,"authority" => $partner->authority,"childs"=>$childs];
        }

        if ($documentdetial->product_id > 1 && empty($memberOrder)) $documentdetial->msg = "<p class='text-danger'>请开通会员后才可访问本文档!</p>";
        if ($documentdetial->product_id > 1 && $product_id < $documentdetial->product_id) $documentdetial->msg = "<p class='text-danger'>会员等级不足,无法查看本文档!</p>";
        $documentdetial->msg = MarkdownEditor::parse($documentdetial->msg);
        return view('admin.documents.documents_detail',['menus'=>$menus,'documents'=>$documents,'documentdetial'=>$documentdetial]);
    }

    public function wechatService(Request $request)
    {
        return view('admin.documents.wechat_service');
    }

    public function documentsAdd(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权添加文章！");
        $helpersLists = [];
        $oldhelpersLists = Documents::getDocumentsTitle();
        foreach ($oldhelpersLists as $oldhelpersList) {
            if ($oldhelpersList->documents_id == "P") {
                $helpersLists[] = $oldhelpersList;
            }
        }
        return view('admin.documents.helpers_add',["helpersLists"=>$helpersLists]);
    }

    public function documentsAddSubmit(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权添加文章！");
        if (empty($request->title)) return back()->with("status","error")->with("msg","请输入文章标题！");
        Documents::helpersInsert($request->title,$request->documents_id,$request->product_id,$request->disable,$request->msg);
        return redirect("/admin/documents/documents_lists")->with("status","success")->with("msg","文章添加成功。");
    }

    public function documentsUpdate(Request $request,$id)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权修改文章！");
        $article = Documents::helpersDetial($id);
        $helpersLists = [];
        $oldhelpersLists = Documents::getDocumentsTitle();
        foreach ($oldhelpersLists as $oldhelpersList) {
            if ($oldhelpersList->documents_id == "P") {
                $helpersLists[] = $oldhelpersList;
            }
        }
        return view('admin.documents.helpers_update',["article"=>$article,"helpersLists"=>$helpersLists]);
    }

    public function documentsUpdateSubmit(Request $request,$id)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权修改文章！");
        if (empty($request->title)) return back()->with("status","error")->with("msg","请输入文章标题！");
        Documents::helpersUpdate($id,$request->title,$request->documents_id,$request->product_id,$request->disable,$request->msg);
        return redirect("/admin/documents/documents_lists")->with("status","success")->with("msg","文章更新成功。");
    }

    public function documentsLists(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权修改文章！");

        $documents = Documents::helpersAdminLists();
        if (empty($documents)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","文档不存在,请检查链接是否正确!");
        $partners = $documents->where("documents_id","P")->all();
        $menus = [];
        foreach ($partners as $partner) {
            $childs = $partners = $documents->where("documents_id",$partner->id)->toArray();
            $menus[] =  ["id" => $partner->id,"title" => $partner->title,"documents_id" => $partner->documents_id,"product_id" => $partner->product_id,"authority" => $partner->authority,"childs"=>$childs];
        }
        return view('admin.documents.helpers_admin',['menus'=>$menus]);
    }

}
